<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/28 00:39
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
class User_Model extends WX_Model
{

    /**
     * 积分记录
     * @param $userid
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public function getScoreRecord($userid, $offset = 0, $limit = 20)
    {
        $db = clone($this->db);
        $db->limit($limit, $offset);
        $db->order_by('created', 'desc');
        $db->where('userid', $userid);
        $query = $db->get('user_score_record');
        return $query->result_array();
    }

    /**
     * 获取用户基本信息
     * @param $userid
     * @return mixed
     */
    public function getUserInfoById($userid)
    {
        $db = clone($this->db);
        $query = $db->get_where('user', ['id' => $userid]);
        $data = $query->row_array();
        if (!empty($data)) {
            $data['amount'] = intval($data['amount']);
            $data['amount_format'] = money_format('%i', $data['amount'] / 100);
            $data['amountFreeze'] = intval($data['amountFreeze']);
            $data['amountFreeze_format'] = money_format('%i', $data['amountFreeze'] / 100);
            $data['amountUnLimit'] = intval($data['amountUnLimit']);
            $data['amountUnLimit_format'] = money_format('%i', $data['amountUnLimit'] / 100);
            $data['amountBuy'] = intval($data['amountBuy']);
            $data['amountBuy_format'] = money_format('%i', $data['amountBuy'] / 100);
            $data['amountCashout'] = intval($data['amountCashout']);
            $data['amountCashout_format'] = money_format('%i', $data['amountCashout'] / 100);
            $data['score'] = intval($data['score']);
        }
        return $data;
    }

    /**
     * 通过openid 获取数据
     * @param $openid
     * @return mixed
     */
    public function getUserSocialInfoByOpenId($openid)
    {
        $db = clone($this->db);
        $query = $db->get_where('user_wx', ['openid' => $openid]);
        return $query->row_array();
    }

    /**
     * 获取用户信息
     * @param $userid
     * @return mixed
     */
    public function getUserProfileById($userid)
    {
        $db = clone($this->db);
        $query = $db->get_where('user_profile', ['userId' => $userid]);
        return $query->row_array();
    }

    /**
     * 保存二维码
     * @param $key
     * @param $value
     * @return mixed
     */
    public function saveQrcodeParams($key, $value, $scene = 'fenxiao')
    {
        $db = clone($this->db);
        $db->insert('user_wx_qrcode', [
            'scene' => 'fenxiao',
            'key' => $key,
            'value' => $value
        ]);
        return $db->insert_id();
    }

    /**
     * 获取二维码参数
     * @param $key
     * @return mixed
     */
    public function getQrcodeParmas($key)
    {
        $db = clone($this->db);
        $query = $db->get_where('user_wx_qrcode', ['key' => $key]);
        return $query->row_array();
    }

    /**
     * 设置值
     * @param $userid
     * @param $billUrl
     * @return mixed
     */
    public function setBillValue($userid, $billUrl)
    {
        $db = clone($this->db);
        $db->where('userId', $userid);
        $db->update('user_profile', ['bill' => $billUrl]);
        return $db->affected_rows();
    }

    /**
     * 下线人数
     * @param $userid
     * @return mixed
     */
    public function getUnderlineCount($userid)
    {
        $db = clone($this->db);
        $db->where(['puserid' => $userid]);
        $puserCount = $db->count_all_results('user_wx_relationship');
        unset($db);

        $db = clone($this->db);
        $db->where('ppuserid', $userid);
        $ppuserCount = $db->count_all_results('user_wx_relationship');
        unset($db);

        return $puserCount + $ppuserCount;
    }

    /**
     * 绑定账号
     * @param $mobile
     * @param $userid
     * @return mixed
     */
    public function bindAccount($mobile, $userid)
    {
        $db = clone($this->db);
        $db->where('id', $userid);
        $db->update('user', ['phone' => $mobile]);
        return $db->affected_rows();
    }

    /**
     * @param $userid
     * @return bool
     */
    public function getMobile($userid)
    {
        $db = clone($this->db);
        $q = $db->get_where('user', ['id' => $userid]);
        $row = $q->row_array();
        if ($row) {
            return $row['phone'];
        }
        return false;
    }

    /**
     * @param $userid
     * @param $password
     * @return mixed
     */
    public function setUserPassword($userid, $password)
    {
        $db = clone($this->db);
        $db->where('id', $userid);
        $db->update('user', [
            'password' => $password['password'],
            'salt' => $password['salt']
        ]);
        return $db->affected_rows();
    }

    /**
     * 是否绑定
     * @param $userid
     * @return bool
     */
    public function checkBind($userid)
    {
        $db = clone($this->db);
        $q = $db->get_where('user', ['id' => $userid]);
        $row = $q->row_array();
        if ($row) {
            return !empty($row['phone']);
        }
        return false;
    }
}